var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,upload = layui.upload
    ,userphotos="",
    form = layui.form,$ = layui.jquery,id=getQueryVariable("id"),
img_count=0,file_count=0;

var tpdata = [{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明"
},{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明"
},{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明"
}];
var fjdata = [{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明",
    "name":"文件名"
},{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明",
    "name":"文件名"
},{
    "url":"statics/images/layui-logo.jpg",
    "shuom":"说明",
    "name":"文件名"
}];

var dw=getdictionary(3,10);//单位
var cgbz=getdictionary(5,14);//采购币种
var gys
let isContainsRoleProduct=false;
getIsContainsRoleProduct();
if (!isContainsRoleProduct){
    document.getElementById("logLi").style.display = "none";
    document.getElementById("supplierLi").style.display = "none";
    document.getElementById("intervalPriceLi").style.display = "none";
}else {
    //页面反应太慢了，控制一下，当有供应商模块时在执行查询
    gys=getgys();
}
var code="";//主产品编码
var cataCode=JSON.parse(window.sessionStorage.getItem("cataCode"));
var hs=getdictionary(3,8);//海关
//表单初始值
if(id!=""){
    var obj=init(id);//回显
    code=obj.code;
    form.val("editor-form",{
        "code":obj.code,
        "chinaName":obj.chinaName,
        "englishName":obj.englishName,
        "customerCode":obj.customerCode,
        "gecode":obj.gecode,
        "ge":obj.ge,
        "bzsm":obj.bzsm,
        "cpjz":obj.cpjz,
        "cpmz":obj.cpmz,
        "cataCode":obj.cataCodeNameCombine,
        "remark":obj.remark
    });
     var hgname="";
    var hg=obj.hgCode;
    for(var i=0;i<hs.length;i++){
        if(hg==hs[i].code){
            hgname=hs[i].name;
            $("input[name='vatRate']")[0].value=hs[i].vatRate;
            $("input[name='taxRate']")[0].value=hs[i].taxRate;
            $("input[name='importTariff']")[0].value=hs[i].importTariff;
            break;
        }else{
            $("input[name='vatRate']")[0].value="";
            $("input[name='taxRate']")[0].value="";
            $("input[name='importTariff']")[0].value="";
        }
    }
    form.val("editor-form",{

         "hgCode":hgname
    });

    tpdata=JSON.parse(obj.img);

    for(var i = 0;i<tpdata.length;i++){
        img_count ++;
        var newImg ='<div class="img_tj">'+
            '<div class="img_wdiv"><img src="'+tpdata[i]+'" id="imgUpload_'+img_count+'"></div>'+
            '<div class="de_div">'+
            /*'<input name="img_instructions'+img_count+'" class="layui-input" placeholder="图片说明" value="'+tpdata[i].shuom+'">'+*/
            '</div>'+
            '</div>';
        $('#addimgdiv').append(newImg);
    }
    fjdata=JSON.parse(obj.wj);
    for(var i = 0;i<fjdata.length;i++){
        file_count ++;
        var filename=fjdata[i].split("filename=")[1];
        var filenames=fjdata[i].split("pathname=")[1].split("&filename=");
        var pathA=filenames[0];//"c:/a/a/"
        var fnas=filenames[1].split("&originalFileName=");
        filename=fnas[1];
        pathA=pathA+"/"+fnas[0];
        var newFile ='<div class="img_tj">'+
            '<div class="file_wdiv"><div style="width: 100%;text-align: center;">' +
            '<a style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="'+filename+'" id="file_span'+file_count+'" download="'+filename+
            '" href="javascript:void(0);" onclick="onLineFile(\''+pathA+'\');">'+filename+'</a> </div>' +
           /* '<a type="button" class="layui-btn" id="fileUpload_'+file_count+'"><i class="layui-icon"></i>上传附件</a></div>'+*/
            '<div class="de_div">'+
           /* '<i class="layui-icon layui-icon-reduce-circle" style="font-size: 30px; color: #000000;line-height: 30px;margin-right:20px;" ' +
            'id="fileUpload_sc_'+file_count+'"></i>'+*/
            '<i class="layui-icon layui-icon-download-circle" style="font-size: 30px; color: #000000;line-height: 30px;" onclick="downFileLoadNew(\''+filename+'\',\''+pathA+'\');"></i>'+
            '</div>'+
            '</div>';
        $('#addfilediv').append(newFile);

    }
    $('#editor-form').find('input,textarea,select').attr('disabled',true);
    form.render();
}

//---产品查询
table.render({
    elem: '#productselect'
    ,type:"get"
    ,url:"productfjinfo/selectByPage"
    ,where:{
        "code":code
    }
    ,toolbar: '#producttoolbar'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field: 'code', title: '规格编码',event:'specificationcoding', width: 120, sort: true}
        ,{field:'categoryName', title:'种类', width: 120, sort: true}
        ,{field:'categoryCode', title:'种类编码', hide: true,  width: 120, sort: true}
        ,{field:'moldCategoryName', title:'模具类别', width: 120, sort: true}
        ,{field:'moldCategoryCode', title:'模具类别编码', hide: true,  width: 120, sort: true}
        ,{field:'firstCategoryName', title:'品类', width: 120, sort: true}
        ,{field:'firstCategoryCode', title:'品类编码', hide: true,  width: 120, sort: true}
        ,{field:'secondaryCategoryName', title:'规格', width: 120, sort: true}
        ,{field:'secondaryCategoryCode', title:'规格编码', hide: true,  width: 120, sort: true}
        ,{field:'threeCategoryName', title:'材质', width: 120, sort: true}
        ,{field:'threeCategoryCode', title:'材质编码', hide: true,  width: 120, sort: true}
        ,{field:'craftName', title:'工艺', width: 120, sort: true}
        ,{field:'craftCode', title:'工艺编码', hide: true,  width: 120, sort: true}
        ,{field:'colorOrderNumberName', title:'颜色订货号', width: 120, sort: true}
        ,{field:'colorOrderNumberCode', title:'颜色订货号编码', hide: true,  width: 120, sort: true}
        ,{field:'privateMoldNumberName', title:'私模订货号', width: 120, sort: true}
        ,{field:'privateMoldNumberCode', title:'私模订货号编码', hide: true,  width: 120, sort: true}
        ,{field:'specialNeedsName', title:'特殊需求', width: 120, sort: true}
        ,{field:'specialNeedsCode', title:'特殊需求编码', hide: true,  width: 120, sort: true}
        ,{field: 'ge', title: '规格说明', width: 120, sort: true}
        ,{field:'chinaRemark', title:'中文描述', width: 120, sort: true}
        ,{field:'englishRemark', title:'英文描述', width: 120, sort: true}
        ,{field:'bzsm', title:'每托箱数', width: 120, sort: true}
        ,{field:'cpjz', title:'净重KGS/CTN', width: 120, sort: true}
        ,{field:'cpmz', title:'毛重KGS/CTN', width: 120, sort: true}
        ,{field: 'colour', title: '颜色', width: 120, sort: true}
        ,{field:'dw', title:'单位', width: 120, sort: true}
        ,{field:'cgbz', title:'采购币种', width: 120, sort: true}
        ,{field:'cgbzws', title:'未税出厂价', width: 120, sort: true}
        ,{field:'cgcb', title:'含税出厂价', width: 120, sort: true}
        ,{field:'fob', title:'含税FOB价', width: 120, sort: true}
        ,{field:'cshh', title:'未税FOB价', width: 120, sort: true}
        ,{field:'wbzc', title:'外包装长mm', width: 120, sort: true}
        ,{field:'wbzk', title:'外包装宽mm', width: 120, sort: true}
        ,{field:'wbzg', title:'外包装高mm', width: 120, sort: true}
        // ,{field:'wbzcc', title:'外包装尺寸', width: 120, sort: true}
        ,{field:'wbzsl', title:'每箱数量', width: 120, sort: true}
        ,{field:'tj', title:'体积', width: 120, sort: true}
        // ,{field:'gys', title:'供应商', width: 120, sort: true}
        ,{field:'hq40zl', title:'40HQ装量', width: 120, sort: true}
        ,{field:'gp20', title:'20GP', width: 120, sort: true}
        ,{field:'gp40', title:'40GP', width: 120, sort: true}
        ,{field:'lclpx', title:'LCL拼箱', width: 120, sort: true}
    ]]
    ,id: 'producttableid'
    ,page: true
    ,height:300
    ,done:function(res, curr, count){

        $("[data-field = 'cgbz']").children().each(function(){
            /*if($(this).text() == '1'){
                $(this).text("共享");
            }*/
            for(var i=0;i<cgbz.length;i++){
                if($(this).text()==cgbz[i].code){
                    $(this).text(cgbz[i].name);
                }
            }
        })
        $("[data-field = 'dw']").children().each(function(){
            /*if($(this).text() == '1'){
                $(this).text("共享");
            }*/
            for(var i=0;i<dw.length;i++){
                if($(this).text()==dw[i].code){
                    $(this).text(dw[i].name);
                }
            }
        })
        $("[data-field = 'gys']").children().each(function(){
            /*if($(this).text() == '1'){
                $(this).text("共享");
            }*/
            for(var i=0;i<gys.length;i++){
                if($(this).text()==gys[i].code){
                    $(this).text(gys[i].name);
                }
            }
        })



    }
});

table.on('tool(producttable)', function(obj){
    var data = obj.data;

    if(obj.event === 'specificationcoding') {

        $("#hidden").show();
        table.render({
            elem: '#productsaselect'
            , type: "get"
            , url: "productfjge/selectByPage"
           // , toolbar: '#productsatoolbar'
            , where: {
                // "productCode": code,
                // "fjCode": data.code
                "zhuge": data.code,
                "mainProductCode": data.productCode
            } //查询的产品ID，条件
           // ,toolbar: '#productsatoolbar'
           // ,where:{id:obj.data.id} //查询的产品ID，条件
            ,cols: [[
                {checkbox: true, fixed: true}
                /*,{field: 'productcode', title: '产品编码', sort: true}
                ,{field: 'specificationcoding', title: '规格编码', sort: true}
                ,{field:'finishedproductratio', title:'成品比例', sort: true}
                ,{field:'accessoriesproportion', title:'配件比例', sort: true}
                ,{field:'chinesename', title:'中文名称', sort: true}
                ,{field:'englishnames', title:'英文名称', sort: true}
                ,{field:'expressaddress', title:'毛重', sort: true}
                ,{field:'netweight', title:'净重', sort: true}
                ,{field:'volume', title:'体积', sort: true}*/
                , {field: 'productCode', title: '产品编码', sort: true}
                , {field: 'fjCode', title: '规格编码', sort: true}
                , {field: 'cpbl', title: '成品比例', sort: true}
                , {field: 'pjbl', title: '配件比例', sort: true}
                , {field: 'chinaname', title: '中文名称', sort: true}
                , {field: 'englishname', title: '英文名称', sort: true}
                , {field: 'mz', title: '毛重', sort: true}
                , {field: 'jz', title: '净重', sort: true}
                , {field: 'tj', title: '体积', sort: true}
            ]]
            ,id: 'productsatableid'
            ,page: true
            ,height:300
        });

        table.render({
            elem: '#asupplierselect'
            , type: "get"
            , url: "productfjgysbak/selectByPage"
            //, toolbar: '#asuppliertoolbar'
            , where: {
                "productCode": code,
                "fjCode": data.code
            } //查询的产品ID，条件
            //,toolbar: '#asuppliertoolbar'
           // ,where:{id:obj.data.id} //查询的产品ID，条件
            ,cols: [[
                {checkbox: true, fixed: true}
                /*,{field: 'asupplier', title: '备选供应商', sort: true}
                ,{field: 'manufacturernumber', title: '厂商货号', sort: true}
                ,{field:'purchasingcurrency', title:'采购币种', sort: true}
                ,{field:'procurementcosts', title:'采购成本', sort: true}
                ,{field:'subordinatedirectory', title:'所属目录', sort: true}*/
                , {field: 'code', title: '供应商', sort: true}
                , {field: 'fob', title: '含税FOB价', sort: true}
                , {field: 'cshh', title: '未税FOB价', sort: true}
                , {field: 'cgbz', title: '采购币种', sort: true}
                , {field: 'cgcb', title: '含税出厂价', sort: true}
                , {field: 'catalog', title: '未税出厂价', sort: true}
                , {field: 'createDate', title: '创建日期', sort: true}
                , {field: 'updateDate', title: '修改日期', sort: true}
            ]]
            ,id: 'asuppliertableid'
            ,page: true
            ,height:300
            , done: function (res, curr, count) {

                $("[data-field = 'cgbz']").children().each(function () {

                    for (var i = 0; i < cgbz.length; i++) {
                        if ($(this).text() == cgbz[i].code) {
                            $(this).text(cgbz[i].name);
                        }
                    }
                })

                $("[data-field = 'code']").children().each(function () {
                    /*if($(this).text() == '1'){
                        $(this).text("共享");
                    }*/
                    for (var i = 0; i < gys.length; i++) {
                        if ($(this).text() == gys[i].code) {
                            $(this).text(gys[i].name);
                        }
                    }
                })

                $("[data-field = 'catalog']").children().each(function () {
                    if ($(this).text() == cataCode) {
                        $(this).text(cataCodeName);
                    }
                })

            }
        });
        table.render({
            elem: '#boxGaugeSelect'
            ,data:[]
            , url: "boxGauge/findBoxGaugeListByPage"
            , toolbar: '#boxGaugeToolbar'
            , where: {
                "productCode": code,
                "specificationCode": data.code,
            }
            , cols: [[
                {checkbox: true, fixed: true}
                , {field: 'productCode', title: '产品编码', hide:true}
                , {field: 'specificationCode', title: '规格编码', hide:true}
                , {field: 'containerCode', title: '货柜型号',hide:true}
                , {field: 'containerName', title: '货柜型号', sort: true}
                , {field: 'boxNum', title: '箱数', sort: true}
                , {field: 'boxGrossWeight', title: '单箱毛重', sort: true}
                , {field: 'boxNetWeight', title: '单箱净重', sort: true}
                , {field: 'boxNumPallet', title: '纸箱数量/托数', sort: true}
                , {field: 'numPallet', title: '数量/托数', sort: true}
                , {field: 'createDate', title: '创建日期', sort: true}
                , {field: 'updateDate', title: '修改日期', sort: true}
            ]]
            , id: 'boxGaugeTableId'
            , page: true
            , height: 300
        });

        table.render({
            elem: '#priceRangeSelect'
            ,data:[]
            , url: "priceRange/findPriceRangeListByPage"
            , toolbar: '#priceRangeToolbar'
            , where: {
                "productCode": code,
                "specificationCode": data.code,
            }
            , cols: [[
                {checkbox: true, fixed: true}
                , {field: 'productCode', title: '产品编码', hide:true}
                , {field: 'specificationCode', title: '规格编码', hide:true}
                , {field: 'supplyName', title: '供应商名称'}
                , {field: 'supplyCode', title: '供应商编码'}
                , {field: 'minNum', title: '最小起订量 ', sort: true}
                , {field: 'maxNum', title: '最大起订量', sort: true}
                , {field: 'taxIncludedFactory', title: '含税出厂价', sort: true}
                , {field: 'unTaxIncludedFactory', title: '未税出厂价', sort: true}
                , {field: 'taxIncludedFob', title: '含税FOB价', sort: true}
                , {field: 'unTaxIncludedFob', title: '未税FOB价', sort: true}
                , {field: 'createDate', title: '创建日期', sort: true}
                , {field: 'updateDate', title: '修改日期', sort: true}
            ]]
            , id: 'priceRangeTableId'
            , page: true
            , height: 300
        });

    }
});

//---日志
/*
table.render({
    elem: '#infoselect'
    ,data:data1
    ,cols: [[
        {field:'operationtype', title:'操作类型', sort: true}
        ,{field:'operationman', title:'操作员名称', sort: true}
        ,{field:'operationdate', title:'更新日期',  sort: true}
        ,{field:'modificationcontent', title:'操作内容', sort: true}
    ]]
    ,id: 'infoselecttableid'
    ,page: true
    ,height:350
});*/
table.render({
    elem: '#infoselect'
    //,data:data1
    ,type:"get"
    ,url:"productlog/selectByPage"
    ,where:{
        "code":code
    }
    ,cols: [[
        {field:'type', title:'操作类型', sort: true}
        ,{field:'optioner', title:'操作员名称', sort: true}
        ,{field:'time', title:'更新日期',  sort: true}
        ,{field:'content', title:'操作内容', sort: true}
    ]]
    ,id: 'infoselecttableid'
    ,page: true
    ,height:350
});
function init(id){
    var obj=[];
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        data:{"id":id},
        url:'productmaininfo/queryById',
        dataType:"json",
        /*
                     contentType: 'application/json',
        */
        async:false,
        success : function (da) {
            if(da.status==200 ){
                obj=da.data;

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return obj;
}


//获取所有供应商
function  getgys() {
    var gys=[];
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        // data:{"ids":JSON.stringify(distributionid),"saleman":data.field.customersalesman},
        url:'customer2/getAllGys',
        dataType:"json",
        /*  contentType: 'application/json',*/
        async:false,
        success : function (da) {
            if(da.status==200 ){
                //gys=da.data;
                for(var i=0;i<da.data.length;i++){
                    var obj={};
                    obj.code=da.data[i].code;
                    obj.name=da.data[i].name;
                    gys.push(obj);
                }

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return gys;
}

function  getoption(objs) {
    var stoptions="<option value=''></option>";
    for(var i=0;i<objs.length;i++){
        stoptions+="<option value='"+objs[i].code+"'>"+objs[i].name+"</option>";
    }
    return stoptions;
}
function getIsContainsRoleProduct(){
    $.ajax({
        url: 'productmaininfo/isContainsRoleProduct',
        async: false, // 同步
        success: function (data) {
            if (data.status==200){
                isContainsRoleProduct=data.data;
            }
        }
    });
}
